package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.DialogChooseSpecBinding
import com.haoshuang.zhouzhoubang.model.bean.GoodsSpecBean
import com.haoshuang.zhouzhoubang.model.bean.ShopDetail
import com.haoshuang.zhouzhoubang.ui.page.shop.detail.GoodsSpecFirstAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.vertical

/**
 * 商品选择弹窗
 * @property spec GoodsSpecBean?
 * @property detail ShopDetail
 * @property binding DialogChooseSpecBinding
 * @property data MutableList<GoodsSpec>
 * @property adapter GoodsSpecFirstAdapter
 * @constructor
 */
class ChooseGoodsSpecDialog(
    context: Context,
    private val spec: GoodsSpecBean?,
    private val detail: ShopDetail?,
    private val onChooseResult: (ids: String, num: Int) -> Unit
) : BottomPopupView(context) {

    private lateinit var binding: DialogChooseSpecBinding

    private val data = spec?.goodsSpecs?.toMutableList() ?: mutableListOf()
    private val adapter = GoodsSpecFirstAdapter(data)

    // 已选数量
    private var chooseNum = 1
    // 已选择的规格id
    private var chooseSpecIds = ""


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_choose_spec
    }

    override fun onCreate() {
        super.onCreate()
        binding = DialogChooseSpecBinding.bind(popupImplView)
        binding.ivImage.load(detail?.image)
        binding.tvSpecPrice.text = "￥${detail?.price}"
        binding.tvChooseSpec.text = "已选: 1件"

        binding.recyclerView.vertical()
            .adapter = adapter

        adapter.onItemChoose = {
            onChooseSpec()
        }

        // 设置选中第一个规格
        data.forEach {
            it.valueList?.first()?.isChoose = true
        }
        adapter.notifyDataSetChanged()
        onChooseSpec()

        binding.tvMinus.isEnabled = false
        binding.tvMinus.clickWithTrigger {
            chooseNum --
            binding.tvGoodsNum.text = "$chooseNum"
            if (chooseNum == 1){
                binding.tvMinus.isEnabled = false
            }
            onChooseSpec()
        }

        binding.tvPlus.clickWithTrigger {
            chooseNum ++
            if (chooseNum > 1){
                binding.tvMinus.isEnabled = true
            }
            binding.tvGoodsNum.text = "$chooseNum"
            onChooseSpec()
        }

        binding.tvSubmit.clickWithTrigger {
            /// 点击提交
//            if (chooseSpecIds.isEmpty()){
//                toast("请选择规格")
//                return@clickWithTrigger
//            }

            dismiss()
            onChooseResult(chooseSpecIds, chooseNum)
        }

    }

    private fun onChooseSpec(){
        val chooseList = arrayListOf<GoodsSpecBean.GoodsSpec.Value?>()
        for (datum in data) {
            val item = datum.valueList?.firstOrNull { it.isChoose == true }
            if (item != null) {
                chooseList.add(item)
            }
        }
        binding.tvChooseSpec.text = "已选: ${chooseNum}件 ${chooseList.joinToString(" ") { it?.title.toString() }}"

        val ids = chooseList.joinToString("_") { it?.id.toString() }
        val data = spec?.table_data?.firstOrNull{ it.id == ids }
        if(data != null){
            chooseSpecIds = ids
            binding.tvSpecPrice.text = "￥${data.product_price}"
        }

    }

}